package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.vo.ChapterVo;
import com.atguigu.eduservice.entity.vo.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.handler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-04-07
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;
    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private VodClient vodClient;
    //根据id查询章节小节
    @Override
    public List<ChapterVo> nestedList(String courseId) {
        //1.最终要得到的数据列表
        ArrayList<ChapterVo>chapterVoArrayList=new ArrayList<>();

        //2.获取章节信息
        QueryWrapper<EduChapter> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("course_id",courseId);

        List<EduChapter> chapters = baseMapper.selectList(queryWrapper1);


        //3.获取小节信息
        QueryWrapper<EduVideo> queryWrapper2  = new QueryWrapper<>();
        queryWrapper2.eq("course_id",courseId);

        List<EduVideo> videos = videoService.list(queryWrapper2);

        //4.遍历封装章节信息
        for (int i = 0; i < chapters.size(); i++) {
            //4.1获取每一个chapter
            EduChapter chapter = chapters.get(i);
            //4.2chapter转换为chapterVo
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);
            chapterVoArrayList.add(chapterVo);
            //5.遍历封装与章节有关的小节信息
            ArrayList<VideoVo>videoVoArrayList=new ArrayList<>();
            for (int m = 0; m < videos.size(); m++) {
                //5.1  获取每一个video
                EduVideo video = videos.get(m);
                //5.2判断与章节的关系
                if (chapter.getId().equals(video.getChapterId())){
                //5.3 video 转换为  videoVo
                VideoVo videoVo = new VideoVo();
                BeanUtils.copyProperties(video,videoVo);
                //5.4封装小节数据
                videoVoArrayList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoArrayList);
        }

        return chapterVoArrayList;
    }

    @Override
    public void delChapterInfoById(String chapterId) {
        // 删除视频
        //1.1查询小节信息
        QueryWrapper<EduVideo> videoIdWrapper = new QueryWrapper<>();
        videoIdWrapper.eq("chapter_id",chapterId);
        List<EduVideo> videoList = videoService.list(videoIdWrapper);
        //1.2遍历集合取出videoId
        List<String>videoIdList=new ArrayList<>();
        for (int i = 0; i < videoList.size(); i++) {
            //1.3封装videoId
            EduVideo eduVideo = videoList.get(i);
            //得到云端视频资源的id并放到集合里
            videoIdList.add(eduVideo.getVideoSourceId());
        }
        //1.4判断集合
        if(videoIdList.size()>0){
            //将多个视频删除
            vodClient.delVideoList(videoIdList);
        }
        //删除小节
        QueryWrapper<EduVideo>videoWrapper=new QueryWrapper<>();
        videoWrapper.eq("chapter_id",chapterId);
        videoService.remove(videoWrapper);
        //删除章节
        QueryWrapper<EduChapter>chapterWrapper=new QueryWrapper<>();
        chapterWrapper.eq("id",chapterId);
        boolean remove = chapterService.remove(chapterWrapper);
        if(!remove){
            throw new GuliException(20001,"删除章节失败");
        }
    }
}
